import userTempId from "@/utils/userTempId";
import {getItem,setItem,removeItem} from "@/utils/localStorage"
import {reqLogin,reqUserMessage} from "@/api"
const state = {
  userTempId: userTempId(),
  token:getItem("token") || "",
  userMessage:{},

};

const mutations = {
// 设置token
set_Token(state,payload){
  state.token = payload.token
  // 永久化存储token
  setItem("token",payload.token)
},
// 设置用户信息
set_UserMessage(state,payload){
  state.userMessage = payload.userMessage
},
// 删除token
remove_Token(state){
  // 删除token
  state.token=""
  // 把本地存储的也删掉
  removeItem("token")

   //清理用户信息
   state.userMessage = {};
},

};
const actions = {
  async getToken({commit},userInfo){
    const result = await reqLogin(userInfo)
    commit("set_Token",{token:result.token})
  },
  async getUserMessage({commit}){
    const result = await reqUserMessage()
    commit ("set_UserMessage",{userMessage:result})
  }
};
const getters = {};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
